// 可以在方法调佣前检查 用户权限 决定是否可以调佣

let users = {
  '001': { roles: ['admin'] },
  '002': { roles: ['member'] },
}

function authorize(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
  const originalMethod = descriptor.value;
  descriptor.value = function (...args: any[]) {
    let user = users[args[0]];
    if(user && user.roles.includes('admin')) {
      originalMethod.apply(this, args);
    } else {
      throw new Error('没有权限');
    }
  }
  return descriptor;
}

class AdminPanel {
  @authorize
  deleteUser(userId: string) {
    console.log(`delete user ${userId}`);
  }
}

const adminPanel= new AdminPanel();
adminPanel.deleteUser("001");
